package org.game.msgBuilder;

import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

import org.base.BaseGoods;
import org.cache.GoodsCfgCache;
import org.cfg.ItemTimeCfg;
import org.constant.ItemType;
import org.game.db.bean.Item;
import org.game.db.bean.Player;
import org.game.db.vo.DressVO;
import org.game.db.vo.EquipmentVO;
import org.game.module.itembo.ItemBO;
import org.game.protobuf.s2c.S2CItemMsg;
import org.game.protobuf.s2c.S2CItemMsg.DressInfo;
import org.game.protobuf.s2c.S2CItemMsg.ItemInfo;
import org.game.protobuf.s2c.S2CItemMsg.ItemList;
import org.game.protobuf.s2c.S2CShareMsg;
import org.game.protobuf.s2c.S2CShareMsg.ChangeType;
import org.game.protobuf.s2c.S2CShareMsg.RewardsMsg;
import org.game.util.ProtoUnit;

import com.sojoys.artifact.manager.MessageManager;
import com.sojoys.artifact.tools.ToolDate;

/**
 * @author : DengYing
 * @CreateDate : 2017年8月15日 下午4:21:17
 * @Description ：Please describe this document
 */
@SuppressWarnings("unchecked")
public class ItemMsgBuilder {

    /**
     * 创建单个道具信息
     */
    public static ItemInfo createItemInfo(ChangeType type, Item item) {
        ItemInfo.Builder builder = ItemInfo.newBuilder();
        long[] out = new long[]{item.getCid(),0L};
        out = createItemTimeMsg(item, out);
        if (out[1] > 0) {
        	builder.setOutTime((int) (out[1] / ToolDate.TIME_SECOND));
		}
        builder.setCt(type);
        builder.setId(Long.toString(item.getId()));
        builder.setCid((int) out[0]);
        builder.setNum(item.getNum());
        return builder.build();
    }

    public static long[] createItemTimeMsg(Item item,long[] out){
    	BaseGoods baseGoods = item.getItemProxy().getCfg();
        if (baseGoods instanceof ItemTimeCfg) {
        	ItemTimeCfg itemTimeCfg = (ItemTimeCfg) baseGoods;
        	out[0] = itemTimeCfg.getItemId();
			switch (itemTimeCfg.getLimitType()) {
			case ItemType.DYNAMIC_TIME_LIMIT:
				out[1] = item.getCreateDate().getTime() + ToolDate.TIME_SECOND * itemTimeCfg.getLimitData();
				break;
			case ItemType.FIXED_TIME_LIMIT:
				out[1] = itemTimeCfg.getLimitData() * ToolDate.TIME_SECOND;
				break;
			}
		}
        return out;
    }
    
    /**
     * 创建单个时装信息
     */
    public static DressInfo createDressInfo(ChangeType type, DressVO dressVO) {
        DressInfo.Builder builder = DressInfo.newBuilder();
        long[] out = new long[]{dressVO.getCid(),0L};
        out = createItemTimeMsg(dressVO, out);
        if (out[1] > 0) {
        	builder.setOutTime((int) (out[1] / ToolDate.TIME_SECOND));
		}
        builder.setCt(type);
        builder.setId(Long.toString(dressVO.getId()));
        builder.setCid((int) out[0]);
        builder.setRoleId(Long.toString(dressVO.getRoleId()));
        
        // 属性
        return builder.build();
    }

    /**
     * 创建道具组信息
     */
    public static ItemList createItemList(ChangeType type, List<Item> items) {
        ItemList.Builder builder = ItemList.newBuilder();
        for (Item item : items) {
            packageItemInfo(type, builder, item);
        }
        // 属性
        return builder.build();
    }

    /**
     * 封装item信息
     */
    public static void packageItemInfo(ChangeType type, ItemList.Builder builder, Item item) {
        BaseGoods goods = GoodsCfgCache.me().get(item.getCid());
        if (goods.getSuperType() == ItemType.DRESS) {
            builder.addDresss(createDressInfo(type, (DressVO) item));
        } else if (goods.getSuperType() == ItemType.EQUIP) {
            builder.addEquipments(createEquipmentInfo(type, (EquipmentVO) item));
        } else {
            builder.addItems(createItemInfo(type, item));
        }
    }

    /**
     * 灵装
     */
    public static S2CItemMsg.EquipmentInfo createEquipmentInfo(ChangeType type, EquipmentVO eq) {
        S2CItemMsg.EquipmentInfo.Builder builder = S2CItemMsg.EquipmentInfo.newBuilder();
        
        long[] out = new long[]{eq.getCid(),0L};
        out = createItemTimeMsg(eq, out);
        if (out[1] > 0) {
        	builder.setOutTime((int) (out[1] / ToolDate.TIME_SECOND));
		}


        
        builder.setCt(type)
                .setId(Long.toString(eq.getId()))
                .setCid((int) out[0])
                .setExp(eq.getExp())
                .setHeroId(Long.toString(eq.getHeroId()))
                .setPosition(eq.getPosition())
                .setLevel(eq.getLevel());
        
        List<List<Integer>> attrList = eq.getSpecialAttrList();

        if (attrList != null) {
            for (int i=0;i<attrList.size();i++){
                List<Integer> subList = attrList.get(i);
                S2CItemMsg.SpecialAttr.Builder attrBuilder = S2CItemMsg.SpecialAttr.newBuilder();
                attrBuilder.setCid(subList.get(EquipmentVO.SPECIAL_ATTR_INDEX_CFG_ID))
                        .setValue(subList.get(EquipmentVO.SPECIAL_ATTR_INDEX_VALUE))
                        .setIndex(i);
                builder.addAttrs(attrBuilder);
            }
        }
        Map tempSpecialAttr = eq.getTempSpecialAttr();
        if (tempSpecialAttr != null){
            List<Integer> newAttr = (List<Integer>) tempSpecialAttr.get(EquipmentVO.NEW_SPECIAL_ATTR_ARR);
            builder.setOldAttrIndex((int) tempSpecialAttr.get(EquipmentVO.OLD_SPECIAL_ATTR_INDEX) + 1);
            builder.setNewAttrType(newAttr.get(EquipmentVO.SPECIAL_ATTR_INDEX_TYPE));
            builder.setNewAttrValue(newAttr.get(EquipmentVO.SPECIAL_ATTR_INDEX_TYPE));
        }
        return builder.build();
    }

    /**
     * 创建奖励消息
     *
     * @param id
     * @param val
     * @return
     */
    public static RewardsMsg createRewardsMsg(int id, int val) {
        return RewardsMsg.newBuilder().setId(id).setNum(val).build();
    }

    // 发送道具信息
    public static void sendItemMsg(Player player, ChangeType type, Item... items) {
        ItemList.Builder builder = ItemList.newBuilder();
        for (Item item : items) {
            if (item == null) {
                continue;
            }
            packageItemInfo(type, builder, item);
        }
        player.getPlayerProxy().sendMsg(MessageManager.me().create(ItemBO.getItems, ProtoUnit.toByte(builder.build())));
    }

    public static void addItemMsg(ItemList.Builder itemChange, Player player, ChangeType type, Item... items) {
        for (Item item : items) {
            packageItemInfo(type, itemChange, item);
        }
    }

}
